package com.leyou.seckill.feign;

import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.constant.LyConstants;
import com.leyou.seckill.scheduled.SeckillTokenScheduled;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class SeckillFeignInterceptor implements RequestInterceptor {
    @Autowired
    private SeckillTokenScheduled seckillTokenScheduled;
    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header(LyConstants.APP_TOKEN_HEADER, seckillTokenScheduled.getToken());
        String userId = String.valueOf(UserHolder.getUserId());
        if (!StringUtils.isEmpty(userId)) {
            requestTemplate.header(LyConstants.USER_HOLDER_KEY, userId);
        }
    }
}
